﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace UITLibraryManagement.Utilities
{
    public class StringUtility
    {
        public static bool IsStringMissing(string value)
        {
            return String.IsNullOrEmpty(value) ||
                value.Trim() == String.Empty;
        }

        public static bool IsValidEmailAddress(string email)
        {
            if (IsStringMissing(email))
                return false;

            // This regex pattern came from: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
            string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";

            return Regex.IsMatch(email, pattern, RegexOptions.IgnoreCase);
        }

        public static bool IsValidPhoneNumber(string value)
        {
            if (IsStringMissing(value))
                return false;
            if (!IsNumber(value))
                return false;
            if (!IsLengthBetween(value, 9, 11))
                return false;
            return true;
        }

        public static bool IsLengthBetween(string value,int min, int max)
        {
            if (value.Length < min || value.Length > max)
                return false;
            return true;
        }

        public static bool IsNumber(string value)
        {
            var result = Int64.MinValue;
            bool parse = Int64.TryParse(value, out result);
            if (!parse)
                return false;
            return true;
        }

        public static bool IsValidCMND(string value)
        {
            if (IsStringMissing(value))
                return false;
            if (!IsNumber(value))
                return false;
            //if (!value.Length.Equals(9))
            //    return false;
            return true;
        }
    }
}
